#!/usr/bin/env python
import gtk, gobject
from error import *
from location import Location
from share import Share
from window_template import WindowTemplate


class LocationWindow(WindowTemplate):
    def __init__(self,location=None):
        """constructor"""    
        WindowTemplate.__init__(self)       
        if location:
            self.location = location
        else:
            self.location = Location("temp")
        self.create_window()        
        self.window.show_all()
        
        
        
    def create_window(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Location Window")
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.global_vbox = gtk.VBox()
        self.window.add(self.global_vbox)
        
        #Name
        self.lbl_name = gtk.Label("Name :")
        self.tb_name = gtk.Entry()
        self.tb_name.set_text(self.location.name)
        self.window.set_focus(self.tb_name)
        self.add_validator(self.tb_name,"REGEX_NAME")
        self.global_vbox.pack_start(self.build_hbox((self.lbl_name,self.tb_name)),False)

        #Params (Drop Down Lists)
        self.params = []
        for param in self.location.param_location:
            for value in self.location.param_location[param]:
                self.add_param_callback(None,param,value)
        self.add_param_callback()
        
        #Submit Button
        submit_button = gtk.Button(None,gtk.STOCK_APPLY)
        submit_button.connect("clicked",self.submit)
        self.global_vbox.pack_end(submit_button,False)
        
        #Automount
        self.cb_automount = gtk.CheckButton("Automount")
        self.global_vbox.pack_end(self.build_hbox((self.cb_automount,)),False)
        
        #Add Param Button
        add_param_button = gtk.Button("+")
        add_param_button.connect("clicked",self.add_param_callback)
        self.global_vbox.pack_end(add_param_button,False)
    
    def __set_param(self,ddl,param):
        ddl.set_active(0)
        if param:
            while ddl.get_active_text() != param:
                ddl.set_active(ddl.get_active() + 1)

    def add_param_callback(self,widget=None,param_type=None,param_value=""):
        ddl_param = gtk.combo_box_new_text()
        for param in self.location.param_location:
            ddl_param.append_text(param)
        self.__set_param(ddl_param,param_type)        
        
        tb_param = gtk.Entry()
        tb_param.set_text(param_value)
        self.global_vbox.pack_start(self.build_hbox((ddl_param,tb_param)),False)
        self.params.append((ddl_param,tb_param))
        self.global_vbox.show_all()
   
    def submit(self,widget=None):
        if self.is_valid():
            self.location.name= self.tb_name.get_text()
            self.location.automount = self.cb_automount.get_active()
            for param in self.params:
                for index in self.location.param_location:
                    if param[0].get_active_text() == index:
                        self.location.add_param(index,param[1].get_text())
            self.window.destroy()
       
    def main(self):
        gtk.main()
        return self.location
        
    def destroy(self,widget):
        gtk.main_quit()
    
    def delete_event(self,widget,event):
        self.location = None
        return False

if __name__ == "__main__":
    base = LocationWindow()
    toto=base.main()
    print toto.name   
